﻿using System.Windows.Controls;
using System.Windows;
using WickedFlame.Core.Layers;

namespace WickedFlame.Util.Controls
{
    [TemplatePart(Name = "PART_ButtonVisible", Type = typeof(Button)), TemplatePart(Name = "PART_ButtonLock", Type = typeof(Button)), TemplatePart(Name = "PART_LayerImage", Type = typeof(LayerImage))]
    public class LayerButton : ContentControl
    {
        Button btnVisible;
        Button btnLock;
        LayerImage layerImage;

        static LayerButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(LayerButton), new FrameworkPropertyMetadata(typeof(LayerButton)));
        }

        public LayerButton()
        {
            Loaded += new RoutedEventHandler(LayerButton_Loaded);
        }

        void LayerButton_Loaded(object sender, RoutedEventArgs e)
        {
            if (Layer == null)
                return;

            Layer.ShapeChanged += new ShapeChangedEventHandler(LayerChanged);
            Layer.ShapeMoved += new ShapeChangedEventHandler(LayerChanged);
            Layer.ShapeAdded += new ShapeChangedEventHandler(LayerChanged);
            Layer.ShapeDeleted += new ShapeChangedEventHandler(LayerChanged);
        }

        public override void OnApplyTemplate()
        {
            if (btnVisible != null)
            {
                btnVisible.Click -= new RoutedEventHandler(btnVisible_click);
                btnVisible = null;
            }
            if (btnLock != null)
            {
                btnLock.Click -= new RoutedEventHandler(btnLock_click);
                btnLock = null;
            }

            base.OnApplyTemplate();

            btnVisible = GetTemplateChild("PART_ButtonVisible") as Button;
            if (btnVisible != null)
                btnVisible.Click += new RoutedEventHandler(btnVisible_click);

            btnLock = GetTemplateChild("PART_ButtonLock") as Button;
            if (btnLock != null)
                btnLock.Click += new RoutedEventHandler(btnLock_click);

			layerImage = GetTemplateChild("PART_LayerImage") as LayerImage;
        }

        private void btnVisible_click(object sender, RoutedEventArgs e)
        {
            //if (layer.Visibility == Visibility.Visible)
            //{
            //    btnVisible.Source = InvisibleImage;
            //    layer.Visibility = Visibility.Hidden;
            //    //selecteable = false;
            //    //layer.Selecteable = false;
            //}
            //else
            //{
            //    btnVisible.Source = VisibleImage;
            //    layer.Visibility = Visibility.Visible;
            //    //layer.Selecteable = true;
            //    //selecteable = true;
            //}

            if (IsVisible)
            {
                IsVisible = false;
                Layer.Visibility = System.Windows.Visibility.Hidden;
            }
            else
            {
                IsVisible = true;
                Layer.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void btnLock_click(object sender, RoutedEventArgs e)
        {
            //if (layer.IsSelecteable)
            //{
            //    btnLock.Source = LockImage;
            //    selecteable = false;
            //    layer.IsSelecteable = false;
            //}
            //else
            //{
            //    btnLock.Source = UnlockImage;
            //    selecteable = true;
            //    layer.IsSelecteable = true;
            //}

            if (IsLocked)
            {
                IsLocked = false;
                Layer.IsSelecteable = true;
            }
            else
            {
                IsLocked = true;
                Layer.IsSelecteable = false;
            }
        }

        private void LayerChanged(object sender, ShapeChangedEventArgs e)
        {
            layerImage.InvalidateVisual();
        }

        public CanvasLayer Layer
        {
            get
            {
                return (CanvasLayer)GetValue(LayerProperty);
            }
            set
            {
                SetValue(LayerProperty, value);
            }
        }

        public static readonly DependencyProperty LayerProperty =
            DependencyProperty.Register("Layer", typeof(CanvasLayer), typeof(LayerButton), new PropertyMetadata(new PropertyChangedCallback(OnLayerPropertyChanged)));//, new CoerceValueCallback(CoerceText), true, UpdateSourceTrigger.PropertyChanged));

        private static void OnLayerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }

        public bool IsSelected
        {
            get
            {
                return (bool)GetValue(IsSelectedProperty);
            }
            set
            {
                SetValue(IsSelectedProperty, value);
            }
        }

        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(LayerButton), new PropertyMetadata(true));

        public new bool IsVisible
        {
            get
            {
                return (bool)GetValue(IsVisibleProperty);
            }
            set
            {
                SetValue(IsVisibleProperty, value);
            }
        }

        public static new readonly DependencyProperty IsVisibleProperty =
            DependencyProperty.Register("IsVisible", typeof(bool), typeof(LayerButton), new PropertyMetadata(true));

        public bool IsLocked
        {
            get
            {
                return (bool)GetValue(IsLockedProperty);
            }
            set
            {
                SetValue(IsLockedProperty, value);
            }
        }

        public static readonly DependencyProperty IsLockedProperty =
            DependencyProperty.Register("IsLocked", typeof(bool), typeof(LayerButton), new PropertyMetadata(false));
    }
}
